package com.ruoyi.iot.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.iot.domain.IotSensor;
import com.ruoyi.iot.enums.SensorTypeEnum;
import com.ruoyi.iot.mapper.IotSensorMapper;
import com.ruoyi.iot.service.IotSensorService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IotSensorImpl extends ServiceImpl<IotSensorMapper, IotSensor> implements IotSensorService {


    @Override
    public void updateDeviceOnlineStatus(String sensorId){
        LambdaQueryWrapper<IotSensor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IotSensor::getSensorId, sensorId);
        IotSensor iotSensor = new IotSensor();
        iotSensor.setOnlineStatus("1");
        iotSensor.setOnlineTime(DateUtils.getNowDate());
        this.update(iotSensor, queryWrapper);
    };

    @Override
    public void updateDeviceOfflineStatus(String sensorId){
        LambdaQueryWrapper<IotSensor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IotSensor::getSensorId, sensorId);
        IotSensor iotSensor = new IotSensor();
        iotSensor.setOnlineStatus("0");
        this.update(iotSensor, queryWrapper);
    };

    @Override
    public void updateSensorOnlineStatusBySensorType(SensorTypeEnum sensorType){
        String sensorTypeCode = sensorType.getSensorType();
        LambdaQueryWrapper<IotSensor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IotSensor::getSensorType, sensorTypeCode);
        IotSensor iotSensor = new IotSensor();
        iotSensor.setOnlineStatus("1");
        iotSensor.setOnlineTime(DateUtils.getNowDate());
        this.update(iotSensor, queryWrapper);
    };
    @Override
    public void updateSensorOfflineStatusBySensorType(SensorTypeEnum sensorType){
        String sensorTypeCode = sensorType.getSensorType();
        LambdaQueryWrapper<IotSensor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IotSensor::getSensorType, sensorTypeCode);
        IotSensor iotSensor = new IotSensor();
        iotSensor.setOnlineStatus("0");
        this.update(iotSensor, queryWrapper);
    };

    @Override
    public void updateSensorWarnStatus(String sensorId, String warningStatus, String lastWarningParam,  String lastWarningContent){
        LambdaQueryWrapper<IotSensor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IotSensor::getSensorId, sensorId);
        IotSensor iotSensor = this.getOne(queryWrapper);
        if(ObjectUtil.equals("0", iotSensor.getWarningStatus())) {
            if( ObjectUtil.equals("1", warningStatus)) {
                iotSensor.setWarningDuration(1);
                iotSensor.setLastWarningTime(DateUtils.getNowDate());
                iotSensor.setLastWarningParam(lastWarningParam);
                iotSensor.setLastWarningContent(lastWarningContent);
            } else {
                iotSensor.setWarningDuration(0);
            }

        } else {
            if( ObjectUtil.equals("1", warningStatus)) {
                iotSensor.setWarningDuration(iotSensor.getWarningDuration() + 1);
                iotSensor.setLastWarningTime(DateUtils.getNowDate());
                iotSensor.setLastWarningParam(lastWarningParam);
                iotSensor.setLastWarningContent(lastWarningContent);
            } else {
                iotSensor.setWarningDuration(0);
            }
        }
        iotSensor.setWarningStatus(warningStatus);
        this.update(iotSensor, queryWrapper);
    };

    @Override
    public void updateSensorWarnStatus(SensorTypeEnum sensorType, String warningStatus,String lastWarningParam){
        String sensorTypeCode = sensorType.getSensorType();
        LambdaQueryWrapper<IotSensor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IotSensor::getSensorType, sensorTypeCode);
        List<IotSensor> iotSensorList = this.list(queryWrapper);
        for(IotSensor iotSensor : iotSensorList) {
            if(ObjectUtil.equals("0", iotSensor.getWarningStatus())) {
                if( ObjectUtil.equals("1", warningStatus)) {
                    iotSensor.setWarningDuration(1);
                    iotSensor.setLastWarningTime(DateUtils.getNowDate());
                    iotSensor.setLastWarningParam(lastWarningParam);
                } else {
                    iotSensor.setWarningDuration(0);
                }

            } else {
                if( ObjectUtil.equals("1", warningStatus)) {
                    iotSensor.setWarningDuration(iotSensor.getWarningDuration() + 1);
                    iotSensor.setLastWarningTime(DateUtils.getNowDate());
                    iotSensor.setLastWarningParam(lastWarningParam);
                } else {
                    iotSensor.setWarningDuration(0);
                }
            }
            iotSensor.setWarningStatus(warningStatus);
            this.updateById(iotSensor);
        }

    };
}
